﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes.sim {
    import flash.events.*;
    import Engine.Managers.*;
    import flash.display.*;
    import flash.geom.*;
    import Engine.Helpers.*;
    import flash.utils.*;
    import Classes.*;
    import Transactions.*;
    import flash.net.*;
    import Engine.*;
    import com.adobe.crypto.*;

    public class WildernessSim {

        private static var m_initialized:Boolean = false;
        private static var m_heatMapInitialized:Boolean = false;

        private const MAX_DISTANCE:int = 9999;
        private const WIDTH:int = 2;
        private const HEIGHT:int = 2;
        private const MAX_DRAWS_PER_FRAME:int = 10;
        private const NUM_WILDERNESS_DATA:int = 20;
        private const MAX_RETRY:int = 3;

        private var m_rollCount:int;
        private var m_regions:XMLList;
        private var m_heatMapPoints:Array;
        private var m_trees:Array;
        private var m_drawObjs:Dictionary;
        private var m_treesDrawn:int = 0;
        private var m_w:int;
        private var m_z:int;
        private var m_loader:URLLoader = null;
        private var m_wildernessData:String;
        private var m_dataNdx:int = 0;
        private var m_retryCount:int = 0;
        private var m_needGeneration:Boolean = false;

        public function WildernessSim(){
            this.m_rollCount = 0;
            this.m_w = int(Global.world.ownerId);
            this.m_z = Number(("0x" + MD5.hash(Global.world.ownerId).substr(0, 8)));
            if (!m_heatMapInitialized){
                this.initializeHeatMap();
                m_heatMapInitialized = true;
            };
            this.m_dataNdx = (Number(Global.player.uid) % this.NUM_WILDERNESS_DATA);
            this.loadWildernessData(this.m_dataNdx);
        }
        protected function loadWildernessData(_arg1:int):void{
            if (_arg1 < 0){
                _arg1 = 0;
            };
            if (_arg1 >= this.NUM_WILDERNESS_DATA){
                _arg1 = (this.NUM_WILDERNESS_DATA - 1);
            };
            var _local2 = (("assets/wilderness/WildernessData/WildernessClientData" + (_arg1 + 1)) + ".txt");
            _local2 = Global.getAssetURL(_local2);
            var _local3:String = AssetUrlManager.instance.lookUpUrl(_local2);
            var _local4:URLRequest = new URLRequest(_local3);
            this.m_loader = new URLLoader();
            this.m_loader.addEventListener(IOErrorEvent.IO_ERROR, this.onDataError);
            this.m_loader.addEventListener(Event.COMPLETE, this.onDataLoaded);
            this.m_loader.load(_local4);
        }
        private function onDataLoaded(_arg1:Event):void{
            if (this.m_loader){
                this.m_loader.removeEventListener(IOErrorEvent.IO_ERROR, this.onDataError);
                this.m_loader.removeEventListener(Event.COMPLETE, this.onDataLoaded);
            };
            this.m_wildernessData = (this.m_loader.data as String);
            this.m_loader = null;
        }
        private function onDataError(_arg1:Event):void{
            if (this.m_loader){
                this.m_loader.removeEventListener(IOErrorEvent.IO_ERROR, this.onDataError);
                this.m_loader.removeEventListener(Event.COMPLETE, this.onDataLoaded);
            };
            this.m_retryCount++;
            if (this.m_retryCount <= this.MAX_RETRY){
                this.loadWildernessData(this.m_dataNdx);
            };
        }
        public function dbgReloadData(_arg1:int):void{
            this.m_dataNdx = _arg1;
            this.m_wildernessData = null;
            this.loadWildernessData(this.m_dataNdx);
            m_initialized = false;
            Global.world.createOverlayBackground();
        }
        public function getNextRandomNumber(_arg1:int, _arg2:int):int{
            this.m_z = ((36969 * (this.m_z & 0xFFFF)) + (this.m_z >> 16));
            this.m_w = ((18000 * (this.m_w & 0xFFFF)) + (this.m_w >> 16));
            var _local3:int = ((this.m_z << 16) + this.m_w);
            var _local4:int = (_arg2 - _arg1);
            _local3 = (Math.abs(_local3) % _local4);
            _local3 = (_local3 + _arg1);
            this.m_rollCount++;
            return (_local3);
        }
        private function getWildernessRect():Object{
            var _local1:Object = new Object();
            var _local2:Rectangle = Global.world.getWorldRect();
            var _local3:int = Global.gameSettings.getAttribute("farmSize");
            var _local4:int = Global.gameSettings.getAttribute("maxFarmSize");
            var _local5:int = Global.gameSettings.getAttribute("wildernessLength");
            _local1["minX"] = -(_local5);
            _local1["minY"] = -(_local5);
            _local1["maxX"] = (_local3 + _local5);
            _local1["maxY"] = (_local3 + _local5);
            return (_local1);
        }
        public function initializeHeatMap():void{
            var _local2:XML;
            var _local3:int;
            var _local4:int;
            var _local5:int;
            var _local6:Object;
            var _local7:int;
            var _local8:int;
            var _local9:int;
			
			// 取xml里的配置
            this.m_regions = Global.gameSettings.getWildernessRegions();
            this.m_heatMapPoints = new Array();
            var _local1:XMLList = Global.gameSettings.getWildernessHeatMap();
            for each (_local2 in _local1) {
                this.m_heatMapPoints.push({
                    x:parseInt(_local2.@x),
                    y:parseInt(_local2.@y)
                });
            };
			
			// 随机生成一些heatMapPoints
            _local3 = Global.gameSettings.getAttribute("wildernessHeatMapMinRandomPoints");
            _local4 = Global.gameSettings.getAttribute("wildernessHeatMapMaxRandomPoints");
            _local5 = this.getNextRandomNumber(_local3, _local4);
            _local6 = this.getWildernessRect();
            _local7 = 0;
            while (_local7 < _local5) {
                _local8 = this.getNextRandomNumber(_local6.minX, _local6.maxX);
                _local9 = this.getNextRandomNumber(_local6.minY, _local6.maxY);
                this.m_heatMapPoints.push({
                    x:_local8,
                    y:_local9
                });
                _local7++;
            };
        }
        public function getTreesInRect(_arg1:Rectangle):Array{
            var _local5:Number;
            var _local6:Number;
            var _local2:Array = new Array();
            var _local3:Rectangle = new Rectangle();
            _local3.width = this.WIDTH;
            _local3.height = this.HEIGHT;
            var _local4:int;
            while (_local4 < this.m_trees.length) {
                _local5 = this.m_trees[_local4].x;
                _local6 = this.m_trees[_local4].y;
                _local3.x = _local5;
                _local3.y = _local6;
                if (_local3.intersects(_arg1)){
                    _local2.push(this.m_trees[_local4]);
                    this.m_trees.splice(_local4, 1);
                    _local4--;
                };
                _local4++;
            };
            return (_local2);
        }
        public function isTreePositionInBounds(_arg1:Array, _arg2:Number, _arg3:Number):Boolean{
            var _local4:Rectangle;
            if (((_arg1) && (_arg1.length))){
                for each (_local4 in _arg1) {
                    if (this.isTreePositionInRect(_local4, _arg2, _arg3)){
                        return (true);
                    };
                };
            };
            return (false);
        }
        public function isTreePositionInRect(_arg1:Rectangle, _arg2:Number, _arg3:Number):Boolean{
            var _local4:Rectangle = new Rectangle();
            _local4.x = _arg2;
            _local4.y = _arg3;
            _local4.width = this.WIDTH;
            _local4.height = this.HEIGHT;
            return (_local4.intersects(_arg1));
        }
        public function removeTreesInRect(_arg1:Rectangle):void{
            var _local5:Number;
            var _local6:Number;
            var _local2:int = this.m_trees.length;
            var _local3:Rectangle = new Rectangle();
            _local3.width = this.WIDTH;
            _local3.height = this.HEIGHT;
            var _local4:int;
            while (_local4 < this.m_trees.length) {
                _local5 = this.m_trees[_local4].x;
                _local6 = this.m_trees[_local4].y;
                _local3.x = _local5;
                _local3.y = _local6;
                if (_local3.intersects(_arg1)){
                    this.m_trees.splice(_local4, 1);
                    _local4--;
                };
                _local4++;
            };
            if (_local2 != this.m_trees.length){
                this.m_treesDrawn = 0;
                Global.world.createOverlayBackground();
            };
        }
        public function removeTreesInUnionedBounds(_arg1:Array):void{
            var _local2:Rectangle;
            var _local3:Rectangle;
            for each (_local3 in _arg1) {
                if (((_local3) && ((_local3 is Rectangle)))){
                    if (!_local2){
                        _local2 = _local3;
                    } else {
                        _local2 = _local2.union(_local3);
                    };
                };
            };
            this.removeTreesInRect(_local2);
        }
        public function getBridgeBounds():Array{
            var _local2:Vector3;
            var _local3:Vector3;
            var _local4:Rectangle;
            var _local5:GameObject;
            var _local6:MapResource;
            var _local1:Array = [];
            var _local7:Array = Global.world.getObjectsByClass(BridgePart).concat(Global.world.getObjectsByClass(Bridge));
            for each (_local5 in _local7) {
                if (_local5){
                    _local6 = (_local5 as MapResource);
                    _local2 = _local6.getPosition();
                    _local3 = _local6.getSize();
                    _local4 = new Rectangle(_local2.x, _local2.y, _local3.x, _local3.y);
                    new Rectangle(_local2.x, _local2.y, _local3.x, _local3.y).y = (_local4.y - 3);
                    _local4.height = (_local4.height + 4);
                    _local4.x = (_local4.x - 3);
                    _local4.width = (_local4.width + 4);
                    _local1.push(_local4);
                };
            };
            return (_local1);
        }
        public function processExpansionsOnObjectPlace(_arg1:MapResource):Array{
            var _local2:Array = [];
            var _local3:Item = _arg1.getItem();
            if (this.hasExpansionGrantInfo(_local3)){
                if (_local3.bridge.grantedExpansionsOnPlace){
                    _local2 = this.onProcessGrantedExpansions(_arg1, _local3.bridge.grantedExpansionsOnPlace, true, _arg1.notifyUserForExpansionsOnPlace);
                };
            };
            return (_local2);
        }
        public function processExpansionsOnConstructionComplete(_arg1:ConstructionSite):Array{
            var _local2:Array = [];
            var _local3:Item = _arg1.targetItem;
            if (this.hasExpansionGrantInfo(_local3)){
                if (_local3.bridge.grantedExpansionsOnFinish){
                    _local2 = this.onProcessGrantedExpansions(_arg1, _local3.bridge.grantedExpansionsOnFinish, false);
                };
            };
            return (_local2);
        }
        public function hasExpansionGrantInfo(_arg1:Item):Boolean{
            var _local2:String;
            var _local3:Item;
            if (!_arg1){
                return (false);
            };
            _local2 = _arg1.bridge.grantedExpansionType;
            if (!_local2){
                return (false);
            };
            _local3 = Global.gameSettings.getItemByName(_local2);
            if (!_local3){
                return (false);
            };
            return (true);
        }
        protected function onProcessGrantedExpansions(_arg1:MapResource, _arg2:String, _arg3:Boolean=true, _arg4:Function=null):Array{
            var _local13:Rectangle;
            var _local14:Rectangle;
            var _local15:Array;
            var _local16:int;
            var _local17:Object;
            var _local18:Wilderness;
            var _local19:int;
            var _local5:Array = [];
            var _local6:Item = ((_arg1 is ConstructionSite)) ? (_arg1 as ConstructionSite).targetItem : _arg1.getItem();
            var _local7:String = _local6.bridge.grantedExpansionType;
            var _local8:Item = Global.gameSettings.getItemByName(_local7);
            var _local9:Vector2 = new Vector2(_arg1.getPosition().x, _arg1.getPosition().y);
            if (_arg3){
                if ((_arg1 is Bridge)){
                    _local13 = new Rectangle(_local9.x, (_local9.y - _local6.bridge.bridgeBoundary.y), _local6.bridge.bridgeBoundary.x, _local6.bridge.bridgeBoundary.y);
                } else {
                    _local13 = new Rectangle(_local9.x, _local9.y, _arg1.getSize().x, _arg1.getSize().y);
                };
                Global.world.wildernessSim.removeTreesInRect(_local13);
            };
            if (!_arg2){
                ErrorManager.addError("No expansions coordinates");
                return (_local5);
            };
            var _local10:Array = _arg2.split("|");
            if ((((_local10.length < 2)) || (!(((_local10.length % 2) == 0))))){
                ErrorManager.addError("Incorrect coordinate sequence for expansion");
                return (_local5);
            };
            var _local11:Boolean;
            var _local12:int;
            while (_local12 < _local10.length) {
                _local14 = new Rectangle(_local10[_local12], _local10[(_local12 + 1)], _local8.expansionWidth, _local8.expansionHeight);
                if (!Global.world.rectIntersectsTerritory(_local14)){
                    Global.world.expandMap(_local14);
                    _local11 = true;
                    if (_arg4 != null){
                        _arg4(_local14);
                    };
                    _local15 = Global.world.wildernessSim.getTreesInRect(_local14);
                    _local16 = (_local15.length - 1);
                    while (_local16 >= 0) {
                        _local17 = _local15[_local16];
                        if (((!(_arg3)) || (!(Global.world.wildernessSim.isTreePositionInRect(_local13, _local17.x, _local17.y))))){
                            _local18 = new Wilderness(_local17.itemName);
                            _local19 = TWorldState.getNextTempID();
                            _local18.setId(_local19);
                            _local17.id = _local19;
                            _local18.setState("static");
                            _local18.setOuter(Global.world);
                            _local18.setDirection(_local17.dir);
                            _local18.setPosition(_local17.x, _local17.y);
                            _local18.isMoveLocked = true;
                            _local18.attach();
                            _local17.wildernessObj = _local18;
                        } else {
                            _local15.splice(_local16, 1);
                        };
                        _local16--;
                    };
                    _local5.push(_local15);
                } else {
                    _local5.push(new Array());
                };
                _local12 = (_local12 + 2);
            };
            if (_local11){
                Global.world.createOverlayBackground();
                Global.world.citySim.roadManager.onGameLoaded(null);
            };
            return (_local5);
        }
        public function gotTrees(_arg1:Number):Boolean{
            if (_arg1 <= 0){
                _arg1 = 0;
            };
            if (_arg1 > 1){
                _arg1 = 1;
            };
            return ((((this.m_treesDrawn > 0)) && ((this.m_treesDrawn >= (this.m_trees.length * _arg1)))));
        }
        public function update():void{
            if (this.m_needGeneration){
                this.generateWilderness();
                return;
            };
            if (this.m_trees == null){
                return;
            };
            if (this.m_treesDrawn >= this.m_trees.length){
                return;
            };
            this.drawTreesInGrassBitmap();
        }
        private function drawTreesInGrassBitmap():void{
            var _local4:Number;
            var _local5:Number;
            var _local6:int;
            var _local7:String;
            var _local8:Wilderness;
            var _local9:DisplayObject;
            var _local10:Number;
            var _local11:Number;
            var _local12:ItemImageInstance;
            var _local13:Bitmap;
            var _local14:CompositeItemImage;
            if (((!(Global.world.canDrawInGrass())) || (!(this.allTreeAssetsLoaded())))){
                return;
            };
            var _local1:int;
            var _local2:Point = IsoMath.tilePosToPixelPos(0, 0);
            var _local3:int;
            while (_local3 < this.m_trees.length) {
                if (_local3 < this.m_treesDrawn){
                } else {
                    this.m_treesDrawn++;
                    _local1++;
                    _local4 = this.m_trees[_local3].x;
                    _local5 = this.m_trees[_local3].y;
                    _local6 = this.m_trees[_local3].dir;
                    _local7 = this.m_trees[_local3].itemName;
                    _local8 = this.m_drawObjs[_local7];
                    if (_local8 == null){
                        break;
                    };
                    _local9 = _local8.getDisplayObject();
                    if (_local9 == null){
                        _local9 = _local8.createDisplayObject();
                        _local8.drawDisplayObject();
                        _local8.updateDisplayObjectTransform();
                    };
                    _local10 = (_local8.getScreenPosition().x - _local2.x);
                    _local11 = (_local8.getScreenPosition().y - _local2.y);
                    _local12 = _local8.getItem().getCachedImage("static");
                    if (_local12 != null){
                        _local13 = null;
                        if ((_local12.image is Bitmap)){
                            _local13 = (_local12.image as Bitmap);
                        } else {
                            if ((_local12.image is CompositeItemImage)){
                                _local14 = (_local12.image as CompositeItemImage);
                                if ((_local14.getBaseImage() is Bitmap)){
                                    _local13 = (_local14.getBaseImage() as Bitmap);
                                };
                            };
                        };
                        Global.world.drawInGrass(_local13, _local4, _local5, _local10, _local11);
                    };
                    if (_local1 > this.MAX_DRAWS_PER_FRAME){
                        break;
                    };
                };
                _local3++;
            };
        }
        public function generateWilderness():void{
            var _local2:Vector3;
            var _local3:Vector3;
            var _local4:Rectangle;
            var _local5:GameObject;
            var _local6:MapResource;
            var _local8:Array;
            var _local9:Road;
            var _local10:Rectangle;
            var _local11:Rectangle;
            var _local19:TrainTracks;
            var _local20:TrainStation;
            var _local21:int;
            var _local22:int;
            var _local23:Boolean;
            var _local24:int;
            var _local25:int;
            var _local26:Boolean;
            var _local27:XML;
            var _local28:int;
            var _local29:Object;
            var _local30:XML;
            var _local31:String;
            if (m_initialized){
                this.m_treesDrawn = 0;
                return;
            };
            if (this.m_wildernessData == null){
                this.m_needGeneration = true;
                return;
            };
            this.m_needGeneration = false;
            var _local1:Array = new Array();
			
			// 取桥的边界
            _local1.concat(this.getBridgeBounds());
            var _local7:Array = Global.world.getObjectsByTypes([WorldObjectTypes.TRAIN_STATION]);
			// 取火车站的边界
            for each (_local5 in _local7) {
                _local19 = (_local5 as TrainTracks);
                _local20 = (_local5 as TrainStation);
                _local6 = (_local5 as MapResource);
                if ((((_local19 == null)) && ((_local20 == null)))){
                } else {
                    _local2 = _local6.getPosition();
                    _local3 = _local6.getSize();
                    _local4 = new Rectangle(_local2.x, _local2.y, _local3.x, _local3.y);
                    new Rectangle(_local2.x, _local2.y, _local3.x, _local3.y).y = (_local4.y - 3);
                    _local4.height = (_local4.height + 4);
                    _local4.x = (_local4.x - 3);
                    _local4.width = (_local4.width + 4);
                    _local1.push(_local4);
                };
            };
			
			// 取路的边界 
            _local8 = Global.world.getObjectsByClass(Road);
            _local10 = new Rectangle();
            _local11 = null;
            for each (_local9 in _local8) {
                _local3 = _local9.getSize();
                _local2 = _local9.getPosition();
                _local10.x = _local2.x;
                _local10.y = _local2.y;
                _local10.width = _local3.x;
                _local10.height = _local3.y;
                if (!Global.world.rectInTerritory(_local10)){
                    if (_local11 == null){
                        _local11 = _local10;
                    } else {
                        _local11 = _local11.union(_local10);
                    };
                };
            };
            if (_local11 != null){
                _local11.y = (_local11.y - 3);
                _local11.height = (_local11.height + 4);
                _local11.x = (_local11.x - 3);
                _local11.width = (_local11.width + 4);
                _local1.push(_local11);
            };
			
			// 把树、草等放到荒野的区域里
            this.m_trees = new Array();
            this.m_treesDrawn = 0;
            var _local12:Object = this.getWildernessRect();
            var _local13:Rectangle = new Rectangle();
            var _local14:int;
            var _local15:int = " ".charCodeAt();
            var _local16:int;
            var _local17:int;
            var _local18:int = (_local12.maxX - this.HEIGHT);
            while (_local18 >= _local12.minX) {
                _local21 = (_local12.maxY - this.HEIGHT);
                while (_local21 >= _local12.minY) {
                    _local22 = this.m_wildernessData.charCodeAt(_local14);
                    _local14++;
                    if (_local22 < 32){
                        _local22 = this.m_wildernessData.charCodeAt(_local14);
                        _local14++;
                    };
                    _local23 = (_local22 > _local15);
                    if (!_local23){
                    } else {
                        _local22 = (_local22 - (_local15 + 1));
                        _local24 = (_local22 & 3);
                        _local25 = ((_local22 >> 2) & 15);
                        if (!Global.world.tileInGroundmap(_local18, _local21)){
                        } else {
                            _local13.x = _local18;
                            _local13.y = _local21;
                            _local13.width = this.WIDTH;
                            _local13.height = this.HEIGHT;
                            if (!Global.world.citySim.waterManager.positionValidForTree(_local13)){
                            } else {
                                _local26 = false;
                                for each (_local4 in _local1) {
                                    if (_local13.intersects(_local4)){
                                        _local26 = true;
                                    };
                                };
                                if (_local26){
                                } else {
                                    _local13.width = 1;
                                    _local13.height = 1;
                                    _local13.x = (_local13.x + 2);
                                    if (Global.world.m_territory.rectInTerritory(_local13)){
                                    } else {
                                        _local13.x = (_local13.x - 2);
                                        _local13.y = (_local13.y + 2);
                                        if (Global.world.m_territory.rectInTerritory(_local13)){
                                        } else {
                                            if ((((_local24 < 0)) || ((_local24 >= this.m_regions.length())))){
                                            } else {
                                                _local27 = this.m_regions[_local24];
                                                _local28 = _local27.asset.length();
                                                if (_local25 >= _local28){
                                                    _local25 = 0;
                                                };
                                                _local30 = _local27.asset[_local25];
                                                _local31 = _local30.@name;
                                                _local29 = {
                                                    x:_local18,
                                                    y:_local21,
                                                    dir:0,
                                                    itemName:_local31
                                                };
                                                this.m_trees.push(_local29);
                                                this.loadTreeAsset(_local29);
                                            };
                                        };
                                    };
                                };
                            };
                        };
                    };
                    _local21 = (_local21 - this.HEIGHT);
                };
                _local18 = (_local18 - this.WIDTH);
            };
            m_initialized = true;
        }
        public function saveWilderness(_arg1:int):void{
            var _local10:int;
            var _local11:Vector3;
            var _local12:Number;
            var _local13:int;
            var _local14:Number;
            var _local15:Number;
            var _local16:int;
            var _local17:int;
            var _local18:XML;
            var _local19:String;
            var _local20:Number;
            var _local21:XMLList;
            var _local22:int;
            var _local23:Boolean;
            var _local24:Vector3;
            var _local25:Number;
            var _local26:XML;
            var _local27:int;
            var _local2 = 1;
            this.m_rollCount = (_arg1 * 0xFF);
            var _local3:Object = this.getWildernessRect();
            var _local4:Rectangle = new Rectangle();
            var _local5:int = " ".charCodeAt();
            var _local6:int;
            var _local7 = "";
            var _local8:int = (_local3.maxX - this.HEIGHT);
            while (_local8 >= _local3.minX) {
                _local10 = (_local3.maxY - this.HEIGHT);
                while (_local10 >= _local3.minY) {
                    _local11 = new Vector3(_local8, _local10);
                    _local6++;
                    if (_local6 > 80){
                        _local7 = (_local7 + "\n");
                        _local6 = 0;
                    };
                    _local12 = this.MAX_DISTANCE;
                    _local13 = 0;
                    while (_local13 < this.m_heatMapPoints.length) {
                        _local24 = _local11.subtract(new Vector3(this.m_heatMapPoints[_local13].x, this.m_heatMapPoints[_local13].y));
                        _local25 = Math.sqrt(_local24.dot(_local24));
                        if (_local25 < _local12){
                            _local12 = _local25;
                            if ((((_local12 < this.WIDTH)) || ((_local12 < this.HEIGHT)))){
                                break;
                            };
                        };
                        _local13++;
                    };
                    _local14 = (this.getNextRandomNumber(0, 100) / 100);
                    _local15 = (this.getNextRandomNumber(0, 100) / 100);
                    _local16 = this.getNextRandomNumber(0, 3);
                    _local17 = this.findRegionNdx(_local12);
                    if ((((_local17 < 0)) || ((_local17 >= this.m_regions.length())))){
                        _local7 = (_local7 + " ");
                    } else {
                        _local18 = this.m_regions[_local17];
                        if (!_local18){
                            _local7 = (_local7 + " ");
                        } else {
                            _local19 = _local18.@density;
                            if (_local14 > parseFloat(_local19)){
                                _local7 = (_local7 + " ");
                            } else {
                                _local20 = 0;
                                _local21 = _local18.asset;
                                _local22 = 0;
                                _local23 = false;
                                _local22 = 0;
                                while (_local22 < _local21.length()) {
                                    _local26 = _local21[_local22];
                                    if (_local15 <= (_local20 + parseFloat(_local26.@probability))){
                                        _local23 = true;
                                        break;
                                    };
                                    _local20 = (_local20 + parseFloat(_local26.@probability));
                                    _local22++;
                                };
                                if (_local23){
                                    _local27 = (((_local5 + 1) + _local17) + (4 * _local22));
                                    _local7 = (_local7 + String.fromCharCode(_local27));
                                } else {
                                    _local7 = (_local7 + " ");
                                };
                            };
                        };
                    };
                    _local10 = (_local10 - this.HEIGHT);
                };
                _local8 = (_local8 - this.WIDTH);
            };
            var _local9:FileReference = new FileReference();
            _local9.save(_local7, (("WildernessClientData" + (_arg1 + 1)) + ".txt"));
        }
        public function findRegion(_arg1:Number):XML{
            var _local3:XML;
            var _local4:Number;
            var _local2:XML;
            for each (_local3 in this.m_regions) {
                _local4 = _local3.@distance;
                if (_arg1 < _local3.@distance){
                    return (_local2);
                };
                _local2 = _local3;
            };
            return (_local2);
        }
        public function findRegionNdx(_arg1:Number):int{
            var _local4:XML;
            var _local5:Number;
            var _local2 = -1;
            var _local3:XML;
            for each (_local4 in this.m_regions) {
                _local5 = _local4.@distance;
                if (_arg1 < _local4.@distance){
                    return (_local2);
                };
                _local3 = _local4;
                _local2++;
            };
            return (_local2);
        }
        public function placeWilderness(_arg1:XML, _arg2:int, _arg3:int, _arg4:Number, _arg5:Number, _arg6:int, _arg7:Shape):Wilderness{
            var _local10:XML;
            var _local11:Object;
            var _local12:String;
            var _local13:Point;
            if (_arg4 > parseFloat(_arg1.@density)){
                return (null);
            };
            var _local8:Number = 0;
            var _local9:XMLList = _arg1.asset;
            for each (_local10 in _local9) {
                if (_arg5 <= (_local8 + parseFloat(_local10.@probability))){
                    if (_arg7 != null){
                        _local13 = IsoMath.tilePosToPixelPos(_arg2, _arg3);
                        _arg7.graphics.moveTo((_local13.x - 1), _local13.y);
                        _arg7.graphics.lineTo((_local13.x + 1), _local13.y);
                        _arg7.graphics.moveTo(_local13.x, (_local13.y - 1));
                        _arg7.graphics.lineTo(_local13.x, (_local13.y + 1));
                    };
                    _local12 = _local10.@name;
                    _local11 = {
                        x:_arg2,
                        y:_arg3,
                        dir:_arg6,
                        itemName:_local12
                    };
                    this.m_trees.push(_local11);
                    this.loadTreeAsset(_local11);
                    return (null);
                };
                _local8 = (_local8 + parseFloat(_local10.@probability));
            };
            return (null);
        }
        public function loadTreeAsset(_arg1:Object):void{
            if (this.m_drawObjs == null){
                this.m_drawObjs = new Dictionary();
            };
            if (this.m_drawObjs.hasOwnProperty(_arg1.itemName)){
                return;
            };
            var _local2:Wilderness = new Wilderness(_arg1.itemName);
            _local2.setState("static");
            _local2.setDirection(0);
            _local2.setPosition(0, 0);
            this.m_drawObjs[_local2.getItemName()] = _local2;
            _local2.loadCurrentImage();
        }
        public function allTreeAssetsLoaded():Boolean{
            var _local1:Object;
            var _local2:Wilderness;
            for (_local1 in this.m_drawObjs) {
                _local2 = (this.m_drawObjs[_local1] as Wilderness);
                if (_local2.isDrawImageLoading()){
                    return (false);
                };
            };
            return (true);
        }

    }
}//package Classes.sim 
